import java.util.Comparator;


public class JobComparator implements Comparator<IJob> 
{

	@Override
	public int compare(IJob arg0, IJob arg1) 
	{		
		char job0_priority = arg0.getJobPriority();
		char job1_priority = arg1.getJobPriority();
		
		int job0_startHour = arg0.getStartHour();
		int job0_startMinute = arg0.getStartMinute();
		int job0_startSecond = arg0.getStartSecond();
		
		int job1_startHour = arg1.getStartHour();
		int job1_startMinute = arg1.getStartMinute();
		int job1_startSecond = arg1.getStartSecond();
		
		if((job0_startHour < 0) && (job1_startHour < 0))	// No start time in both jobs
		{
			if(isGreater(job0_priority,job1_priority))		// Then sort according to their priority
				return 1;
			else if(isGreater(job1_priority,job0_priority))
				return -1;
			else
				return 0;			
		}
		
		else if((job0_startHour < 0) && (job1_startHour >= 0))	// Start time in job 1 only
			return 1;
		else if((job0_startHour >= 0) && (job1_startHour < 0))	// Start time in job 0 only
			return -1;
		else
		{
			if(job0_startHour > job1_startHour)		// Job0's start time is strictly greater than that of Job1
				return -1;
			
			else if(job0_startHour < job1_startHour)
				return 1;
			else	
			{
				if(job0_startMinute > job1_startMinute)
					return -1;
				else if(job0_startMinute < job1_startMinute)
					return 1;
				else
				{
					if(job0_startSecond > job1_startSecond)
						return -1;
					else if(job0_startSecond < job1_startSecond)
						return 1;
					else
					{
						if(isGreater(job0_priority,job1_priority))		// Then sort according to their priority
							return 1;
						else if(isGreater(job1_priority,job0_priority))
							return -1;
						else
							return 0;	
					}
				}
			}
		}
	}
	
	private static boolean isGreater(char p1, char p2)
	{
		if(p1 == 'L')
			return false;
		else if(p1 == 'M')
		{
			if(p2 == 'L')
				return true;
			else
				return false;
		}
		
		else
		{
			if(p2 == 'H')
				return false;
			else
				return true;
		}
	}
}
